package com.base.myproject.client.HR.attendance;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseTreeStore;
import com.base.myproject.client.busi.Editdisable;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.BO.TreeNodeModelData;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.tools.CommandSyncsql;
import com.base.myproject.client.tools.DateUtil;
import com.base.myproject.client.tools.GreetingService;
import com.base.myproject.client.tools.GreetingServiceAsync;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.event.TreePanelEvent;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.CheckBoxGroup;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.DateTimePropertyEditor;
import com.extjs.gxt.ui.client.widget.form.StoreFilterField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanelSelectionModel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckCascade;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckNodes;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.URL;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Label;


/**
 * 
 * @author 排班
 * 
 */
public class Scheduling extends BaseJDBCPanel {
	// 创建一个TabPanel
	String code = "";

	String tree_depcode = null;// 讲当前树形结构中选中的部门id保存
	String tree_de_name = null;// 讲当前树形结构中选中的部门name保存

	TabPanel tabPanel = null;
	TreePanel<TreeNodeModelData> treePanel = null;
	// 创建树形结构的数据表
	BaseTreeStore<TreeNodeModelData> treeStore = null;

	Button seach_bt = new Button("查询");// 查询按钮
	TextField<String> seach_name = new TextField<String>();// 姓名输入框
	DateField seach_by_date = new DateField();// 查询日期

	Button get = new Button("批量排班");
	Button setweek = new Button("添加休息日");
	Button add_p = new Button("新增人员");
	Button copy_p = new Button("继承上月排班");  //暂时不添加到页面上！因为酒店没作用
	StoreFilterField<BusiObject> field;// 类似ajax的自动查询框
	CheckBoxGroup checkGroup = new CheckBoxGroup();
	CheckBox checkBox8 = new CheckBox();// 法定节假日

	TextField<String> TextField_temp = new TextField<String>();// grid中的text公共变量
	List<String>  copy_array=new ArrayList<String>();
	
	private String bcode = Env.getEnvbcode();
	private String results = null;
	private List<String> sqlList = new ArrayList<String>();;
	
	BaseComboBoxX box;
	
	public Scheduling() {
		seach_by_date.setPropertyEditor(new DateTimePropertyEditor("yyyy-MM"));
		seach_by_date.setName("seach_ym");
		seach_by_date.setFormatValue(false);
		seach_by_date.setWidth(126);
		
		
		String sql = "select substring('id like '''+replace(permissionsbcode,';','%'' or id like '''),9," +
		"len('id like '''+replace(permissionsbcode,';','%'' or id like '''))-21) as result from" +
		" busipermissions where bcode = '"+bcode+"' and busicode = 8000";
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService2.getDataSet(sql, new AsyncCallback<DataSet>(){
		@Override
			public void onFailure(Throwable caught) {	
			}
			@Override
			public void onSuccess(DataSet result) {  
				if(result.getRowCount() != 0){
					results = result.getValue(0, "result");
					init();
				}else{
					MessageBox.alert("提示", "抱歉，您没有查看权限", null);
				}
			}
		});
	}

	public void init() {
		initTreePanel();
		initWeigetx();
		registerMainStore(store);
		registerStore(store);
	}

	public void complete() {
		super.complete();
		print.addListener(Events.Select, getListener());
		getToolbarll().add(print);
	}
	
	ContentPanel leftCenter;

	private void initWeigetx() {
	
		// 垂直布局
		VerticalPanel vp=new VerticalPanel();
		vp.setSpacing(3);
	
		
		HorizontalPanel s=new HorizontalPanel();
		s.setHeight("25");
		s.setVerticalAlign(VerticalAlignment.MIDDLE);
		s.add(new Label(" 关键字查询: "));
		cread_field();
		s.add(field);
		s.add(new Label("　　排班年月: "));

		s.add(seach_by_date);

		seach_bt.addListener(Events.Select, getListener());
		s.add(seach_bt);
		get.addListener(Events.Select, getListener());
		s.add(get);
		add_p.addListener(Events.Select, getListener());
		s.add(add_p);
		copy_p.addListener(Events.Select, getListener());
		s.add(copy_p);
		vp.add(s);

		HorizontalPanel w = new HorizontalPanel();
		w.setVerticalAlign(VerticalAlignment.MIDDLE);
		CheckBox checkBox0 = new CheckBox();
		CheckBox checkBox1 = new CheckBox();
		CheckBox checkBox2 = new CheckBox();
		CheckBox checkBox3 = new CheckBox();
		CheckBox checkBox4 = new CheckBox();
		CheckBox checkBox5 = new CheckBox();
		CheckBox checkBox6 = new CheckBox();
		CheckBox checkBox7 = new CheckBox();

		w.add(new Label(" 指定休息日: "));

		checkBox0.setBoxLabel("星期0");
		checkBox0.setVisible(false);
		checkBox1.setBoxLabel("星期一");
		checkBox2.setBoxLabel("星期二");
		checkBox3.setBoxLabel("星期三");
		checkBox4.setBoxLabel("星期四");
		checkBox5.setBoxLabel("星期五");
		checkBox6.setBoxLabel("星期六");
		checkBox6.setValue(true);
		checkBox7.setBoxLabel("星期日");
		checkBox7.setValue(true);
		checkBox8.setValue(true);
		checkBox8.setBoxLabel("法定节日");

		checkGroup.setFieldLabel("指定休息日");

		checkGroup.add(checkBox0);
		checkGroup.add(checkBox1);
		checkGroup.add(checkBox2);
		checkGroup.add(checkBox3);
		checkGroup.add(checkBox4);
		checkGroup.add(checkBox5);
		checkGroup.add(checkBox6);
		checkGroup.add(checkBox7);
		checkGroup.setHeight(22);
		w.add(checkGroup);
		w.add(checkBox8);

		HorizontalPanel workBatch = new HorizontalPanel();
		workBatch.setSpacing(2);
		
		workBatch.add(create_workBatch_grid());
		workBatch.add(create_publicWorkBatch_grid());
		
		vp.add(getBaseGrid());
		vp.add(workBatch);
		vp.add(w);

		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 5, 5, 0));

		ContentPanel center = new ContentPanel();
		center.setHeaderVisible(false);
		Tools.setResize(vp, 0.99, 0.90);
		center.add(vp);
		
		BorderLayoutData northData = new BorderLayoutData(LayoutRegion.WEST,200);
		northData.setSplit(true);
		northData.setCollapsible(true);
		northData.setMargins(new Margins(5, 0, 5, 0));

		ContentPanel panel = new ContentPanel();
		panel.setHeading("选择部门");
		panel.add(getTreePanel());
		
		BorderLayout layout = new BorderLayout();
		this.setLayout(layout);
		add(panel, northData);
		add(center, centerData);
		this.layout();
	}
	TreePanel<TreeNodeModelData> getTreePanel() {
		if (treePanel == null) {
			initTreePanel();
		}
		return treePanel;
	}

	BaseTreeStore<TreeNodeModelData> getTreeStore() {
		if (treeStore == null) {
			initTreeStore();
		}
		return treeStore;
	}

	// 创建数据连接表
	BaseTreeStore<TreeNodeModelData> initTreeStore() {
		StoreInfo si = new StoreInfo();
		si.setSql("select icode,id,name from SystemInfo where id like "+results+" order by id asc");
		si.setTreesortcolumn("id");
		si.setKeyname(new String[] { "icode" });
		si.setTablename("SystemInfo");
		treeStore = new BaseTreeStore<TreeNodeModelData>(si, false);
		treeStore.load();
		return treeStore;

	}

	// 实例化TreePanel
	TreePanelSelectionModel<TreeNodeModelData> tpsm = new TreePanelSelectionModel<TreeNodeModelData>();

	void initTreePanel() {
		treePanel = new TreePanel<TreeNodeModelData>(getTreeStore());
		treePanel.setWidth(200);
		treePanel.setHeight(465);
		treePanel.setDisplayProperty("name");
		treePanel.setSelectionModel(tpsm);
		treePanel.setCheckStyle(CheckCascade.PARENTS);
		treePanel.getStyle().setLeafIcon(AbstractImagePrototype.create(BaseResource.INSTANCE
				.tree_file())); 
		treePanel.setCheckNodes(CheckNodes.BOTH);
		treePanel.setCheckable(false);
		treePanel.setAutoLoad(true);

		treePanel.addListener(Events.OnClick,
				new Listener<TreePanelEvent<TreeNodeModelData>>() {
					public void handleEvent(TreePanelEvent<TreeNodeModelData> be) {
						set_grid(be);
					}
				});
	}

	public void setCheckNodes(CheckNodes checkNodes) {
		treePanel.setCheckNodes(checkNodes);
	}

	private void setGridField() {
		int month = seach_by_date.getValue().getMonth() + 1;
		if (month == 4 || month ==6 || month == 9 || month == 11) {
			C36.setHidden(true);
		} else if (month == 2) {
			int year = seach_by_date.getValue().getYear();
			if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
				C34.setHidden(false);
			} else {
				C34.setHidden(true);
			}
			C35.setHidden(true);
			C36.setHidden(true);
		} else {
			C34.setHidden(false);
			C35.setHidden(false);
			C36.setHidden(false);
		}
	}
	
	void set_grid(TreePanelEvent<TreeNodeModelData> be) {
		String id = null;
		if (be.getItem() == null) return;
		id = be.getItem().get("id").toString();
		tree_depcode = id;// 讲当前树形结构中选中的部门id保存
		tree_de_name = be.getItem().get("name").toString();

		grid.getStore().getStoreinfo().setSql("select * from hr_Attendance_schedule where 1=2");
		grid.getStore().reload();
		
		box
		.setFilter(" WHERE b.bcode = a.bcode and b.employstatus = '在职' and a.systemid like '"
				+ tree_depcode + "%25'");

		// 配置班次表更新
		String sql_old = workBatch_grid.getStore().getStoreinfo().getSql();
		String sql_new = "select * from hr_Attendance_WorkBatch where depCode like '"
				+ id + "%25'";

		if (!sql_old.equals(sql_new)) {
			StoreInfo info3 = new StoreInfo();
			info3.setTablename("hr_Attendance_WorkBatch");
			info3.setKeyname(new String[] { "icode" });
			info3.setSql(sql_new);
			workBatch_grid.getStore().setStoreinfo(info3);
			workBatch_grid.getStore().reload();
		}
	}

	// 创建数据表
	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	ModelType type_m = new ModelType();

	void createStore() {
		type_m.setRoot("records");
		type_m.setRecordName("record");
		type_m.setTotalName("totle");
		type_m.addField("icode");
		type_m.getField("icode").setType(Long.class);
		type_m.addField("bcode");
		type_m.getField("bcode").setType(String.class);
		type_m.addField("bname");
		type_m.getField("bname").setType(String.class);
		type_m.addField("depCode");
		type_m.getField("depCode").setType(String.class);
		type_m.addField("ym");
		type_m.getField("ym").setType(Date.class);
		type_m.addField("r1_r");
		type_m.getField("r1_r").setType(Boolean.class);
		type_m.addField("r1");
		type_m.getField("r1").setType(String.class);
		type_m.addField("r2_r");
		type_m.getField("r2_r").setType(Boolean.class);
		type_m.addField("r2");
		type_m.getField("r2").setType(String.class);
		type_m.addField("r3_r");
		type_m.getField("r3_r").setType(Boolean.class);
		type_m.addField("r3");
		type_m.getField("r3").setType(String.class);
		type_m.addField("r4_r");
		type_m.getField("r4_r").setType(Boolean.class);
		type_m.addField("r4");
		type_m.getField("r4").setType(String.class);
		type_m.addField("r5_r");
		type_m.getField("r5_r").setType(Boolean.class);
		type_m.addField("r5");
		type_m.getField("r5").setType(String.class);
		type_m.addField("r6_r");
		type_m.getField("r6_r").setType(Boolean.class);
		type_m.addField("r6");
		type_m.getField("r6").setType(String.class);
		type_m.addField("r7_r");
		type_m.getField("r7_r").setType(Boolean.class);
		type_m.addField("r7");
		type_m.getField("r7").setType(String.class);
		type_m.addField("r8_r");
		type_m.getField("r8_r").setType(Boolean.class);
		type_m.addField("r8");
		type_m.getField("r8").setType(String.class);
		type_m.addField("r9_r");
		type_m.getField("r9_r").setType(Boolean.class);
		type_m.addField("r9");
		type_m.getField("r9").setType(String.class);
		type_m.addField("r10_r");
		type_m.getField("r10_r").setType(Boolean.class);
		type_m.addField("r10");
		type_m.getField("r10").setType(String.class);
		type_m.addField("r11_r");
		type_m.getField("r11_r").setType(Boolean.class);
		type_m.addField("r11");
		type_m.getField("r11").setType(String.class);
		type_m.addField("r12_r");
		type_m.getField("r12_r").setType(Boolean.class);
		type_m.addField("r12");
		type_m.getField("r12").setType(String.class);
		type_m.addField("r13_r");
		type_m.getField("r13_r").setType(Boolean.class);
		type_m.addField("r13");
		type_m.getField("r13").setType(String.class);
		type_m.addField("r14_r");
		type_m.getField("r14_r").setType(Boolean.class);
		type_m.addField("r14");
		type_m.getField("r14").setType(String.class);
		type_m.addField("r15_r");
		type_m.getField("r15_r").setType(Boolean.class);
		type_m.addField("r15");
		type_m.getField("r15").setType(String.class);
		type_m.addField("r16_r");
		type_m.getField("r16_r").setType(Boolean.class);
		type_m.addField("r16");
		type_m.getField("r16").setType(String.class);
		type_m.addField("r17_r");
		type_m.getField("r17_r").setType(Boolean.class);
		type_m.addField("r17");
		type_m.getField("r17").setType(String.class);
		type_m.addField("r18_r");
		type_m.getField("r18_r").setType(Boolean.class);
		type_m.addField("r18");
		type_m.getField("r18").setType(String.class);
		type_m.addField("r19_r");
		type_m.getField("r19_r").setType(Boolean.class);
		type_m.addField("r19");
		type_m.getField("r19").setType(String.class);
		type_m.addField("r20_r");
		type_m.getField("r20_r").setType(Boolean.class);
		type_m.addField("r20");
		type_m.getField("r20").setType(String.class);
		type_m.addField("r21_r");
		type_m.getField("r21_r").setType(Boolean.class);
		type_m.addField("r21");
		type_m.getField("r21").setType(String.class);
		type_m.addField("r22_r");
		type_m.getField("r22_r").setType(Boolean.class);
		type_m.addField("r22");
		type_m.getField("r22").setType(String.class);
		type_m.addField("r23_r");
		type_m.getField("r23_r").setType(Boolean.class);
		type_m.addField("r23");
		type_m.getField("r23").setType(String.class);
		type_m.addField("r24_r");
		type_m.getField("r24_r").setType(Boolean.class);
		type_m.addField("r24");
		type_m.getField("r24").setType(String.class);
		type_m.addField("r25_r");
		type_m.getField("r25_r").setType(Boolean.class);
		type_m.addField("r25");
		type_m.getField("r25").setType(String.class);
		type_m.addField("r26_r");
		type_m.getField("r26_r").setType(Boolean.class);
		type_m.addField("r26");
		type_m.getField("r26").setType(String.class);
		type_m.addField("r27_r");
		type_m.getField("r27_r").setType(Boolean.class);
		type_m.addField("r27");
		type_m.getField("r27").setType(String.class);
		type_m.addField("r28_r");
		type_m.getField("r28_r").setType(Boolean.class);
		type_m.addField("r28");
		type_m.getField("r28").setType(String.class);
		type_m.addField("r29_r");
		type_m.getField("r29_r").setType(Boolean.class);
		type_m.addField("r29");
		type_m.getField("r29").setType(String.class);
		type_m.addField("r30_r");
		type_m.getField("r30_r").setType(Boolean.class);
		type_m.addField("r30");
		type_m.getField("r30").setType(String.class);
		type_m.addField("r31_r");
		type_m.getField("r31_r").setType(Boolean.class);
		type_m.addField("r31");
		type_m.getField("r31").setType(String.class);
		type_m.addField("readOnly");
		type_m.getField("readOnly").setType(String.class);
		type_m.addField("average_length");
		type_m.getField("average_length").setType(Double.class);

		StoreInfo info = new StoreInfo();
		info.setTablename("hr_Attendance_schedule");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from hr_Attendance_schedule where 1=2");// 初始化的时候只显示当前月份的数据

		store = new BaseStore<BusiObject>(type_m, info);
		store.addEditdisableItem(new Editdisable("readOnly", "1",
				BaseStore.EditDisableType.IN));
	}

	// 创建一个grid
	BaseGrid<BusiObject> grid = null;

	BaseGrid<BusiObject> getBaseGrid() {
		if (grid == null) {
			createGrid();
		}
		return grid;
	}

	/**
	 * 为排班表定义31个text变量
	 */
	TextField<String> tC6 = new TextField<String>();
	TextField<String> tC7 = new TextField<String>();
	TextField<String> tC8 = new TextField<String>();
	TextField<String> tC9 = new TextField<String>();
	TextField<String> tC10 = new TextField<String>();
	TextField<String> tC11 = new TextField<String>();
	TextField<String> tC12 = new TextField<String>();
	TextField<String> tC13 = new TextField<String>();
	TextField<String> tC14 = new TextField<String>();
	TextField<String> tC15 = new TextField<String>();
	TextField<String> tC16 = new TextField<String>();
	TextField<String> tC17 = new TextField<String>();
	TextField<String> tC18 = new TextField<String>();
	TextField<String> tC19 = new TextField<String>();
	TextField<String> tC20 = new TextField<String>();
	TextField<String> tC21 = new TextField<String>();
	TextField<String> tC22 = new TextField<String>();
	TextField<String> tC23 = new TextField<String>();
	TextField<String> tC24 = new TextField<String>();
	TextField<String> tC25 = new TextField<String>();
	TextField<String> tC26 = new TextField<String>();
	TextField<String> tC27 = new TextField<String>();
	TextField<String> tC28 = new TextField<String>();
	TextField<String> tC29 = new TextField<String>();
	TextField<String> tC30 = new TextField<String>();
	TextField<String> tC31 = new TextField<String>();
	TextField<String> tC32 = new TextField<String>();
	TextField<String> tC33 = new TextField<String>();
	TextField<String> tC34 = new TextField<String>();
	TextField<String> tC35 = new TextField<String>();
	TextField<String> tC36 = new TextField<String>();
	
	ColumnConfig C34;
	ColumnConfig C35;
	ColumnConfig C36;
	MenuItem cut = new MenuItem();
	ColumnConfig C2;
	List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
	boolean delete = false;
	String bcodesDel = " and icode in ( ";
	
	void createGrid() {
		CheckBoxSelectionModel<BusiObject> cbs = new CheckBoxSelectionModel<BusiObject>();
		columns.add(cbs.getColumn());
		
		C2 = new ColumnConfig("bcode","雇佣编号",80);
		BaseComboBoxForm<BusiObject> employno = new BaseComboBoxForm<BusiObject>("bcode","employno","bcode_job"," where employstatus = '在职'",true); 
		C2.setRenderer(employno);
		columns.add(C2);
		
		ColumnConfig C3 = new ColumnConfig("bname", "姓名", 60);
		box = new BaseComboBoxX("a.bname", "a.bcode", "bcode a, bcode_job b",
				" WHERE b.bcode = a.bcode and b.employstatus = '在职' and a.systemid like '"
				+ tree_depcode + "%25'");
		box.addListener(Events.BeforeQuery, getListener());
		box.addListener(Events.Changed, getListener());
		C3.setEditor(box.getCellEditor());
		box.setReadOnly(true);
		columns.add(C3);
		
		ColumnConfig C4 = new ColumnConfig("ym", "年月", 70);
		C4.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM"));

		columns.add(C4);

		int cw = 30;
		ColumnConfig C6 = new ColumnConfig("r1", "1", cw);
		tC6.setName("r1");
		tC6.setFireChangeEventOnSetValue(true);
		C6.setEditor(new CellEditor(tC6));
		columns.add(C6);

		ColumnConfig C7 = new ColumnConfig("r2", "2", cw);
		tC7.setName("r2");
		tC7.setFireChangeEventOnSetValue(true);
		C7.setEditor(new CellEditor(tC7));
		columns.add(C7);

		ColumnConfig C8 = new ColumnConfig("r3", "3", cw);
		tC8.setName("r3");
		tC8.setFireChangeEventOnSetValue(true);
		C8.setEditor(new CellEditor(tC8));
		columns.add(C8);

		ColumnConfig C9 = new ColumnConfig("r4", "4", cw);
		tC9.setName("r4");
		C9.setEditor(new CellEditor(tC9));
		columns.add(C9);

		ColumnConfig C10 = new ColumnConfig("r5", "5", cw);
		tC10.setName("r5");
		C10.setEditor(new CellEditor(tC10));
		columns.add(C10);

		ColumnConfig C11 = new ColumnConfig("r6", "6", cw);
		tC11.setName("r6");
		C11.setEditor(new CellEditor(tC11));
		columns.add(C11);

		ColumnConfig C12 = new ColumnConfig("r7", "7", cw);
		tC12.setName("r7");
		C12.setEditor(new CellEditor(tC12));
		columns.add(C12);

		ColumnConfig C13 = new ColumnConfig("r8", "8", cw);
		tC13.setName("r8");
		C13.setEditor(new CellEditor(tC13));
		columns.add(C13);

		ColumnConfig C14 = new ColumnConfig("r9", "9", cw);
		tC14.setName("r9");
		C14.setEditor(new CellEditor(tC14));
		columns.add(C14);

		ColumnConfig C15 = new ColumnConfig("r10", "10", cw);
		tC15.setName("r10");
		C15.setEditor(new CellEditor(tC15));
		columns.add(C15);

		ColumnConfig C16 = new ColumnConfig("r11", "11", cw);
		tC16.setName("r11");
		C16.setEditor(new CellEditor(tC16));
		columns.add(C16);

		ColumnConfig C17 = new ColumnConfig("r12", "12", cw);
		tC17.setName("r12");
		C17.setEditor(new CellEditor(tC17));
		columns.add(C17);

		ColumnConfig C18 = new ColumnConfig("r13", "13", cw);
		tC18.setName("r13");
		C18.setEditor(new CellEditor(tC18));
		columns.add(C18);

		ColumnConfig C19 = new ColumnConfig("r14", "14", cw);
		tC19.setName("r14");
		C19.setEditor(new CellEditor(tC19));
		columns.add(C19);

		ColumnConfig C20 = new ColumnConfig("r15", "15", cw);
		tC20.setName("r15");
		C20.setEditor(new CellEditor(tC20));
		columns.add(C20);

		ColumnConfig C21 = new ColumnConfig("r16", "16", cw);
		tC21.setName("r16");
		C21.setEditor(new CellEditor(tC21));
		columns.add(C21);

		ColumnConfig C22 = new ColumnConfig("r17", "17", cw);
		tC22.setName("r17");
		C22.setEditor(new CellEditor(tC22));
		columns.add(C22);

		ColumnConfig C23 = new ColumnConfig("r18", "18", cw);
		tC23.setName("r18");
		C23.setEditor(new CellEditor(tC23));
		columns.add(C23);

		ColumnConfig C24 = new ColumnConfig("r19", "19", cw);
		tC24.setName("r19");
		C24.setEditor(new CellEditor(tC24));
		columns.add(C24);

		ColumnConfig C25 = new ColumnConfig("r20", "20", cw);
		tC25.setName("r20");
		C25.setEditor(new CellEditor(tC25));
		columns.add(C25);

		ColumnConfig C26 = new ColumnConfig("r21", "21", cw);
		tC26.setName("r21");
		C26.setEditor(new CellEditor(tC26));
		columns.add(C26);

		ColumnConfig C27 = new ColumnConfig("r22", "22", cw);
		tC27.setName("r22");
		C27.setEditor(new CellEditor(tC27));
		columns.add(C27);

		ColumnConfig C28 = new ColumnConfig("r23", "23", cw);
		tC28.setName("r23");
		C28.setEditor(new CellEditor(tC28));
		columns.add(C28);

		ColumnConfig C29 = new ColumnConfig("r24", "24", cw);
		tC29.setName("r24");
		C29.setEditor(new CellEditor(tC29));
		columns.add(C29);

		ColumnConfig C30 = new ColumnConfig("r25", "25", cw);
		tC30.setName("r25");
		C30.setEditor(new CellEditor(tC30));
		columns.add(C30);

		ColumnConfig C31 = new ColumnConfig("r26", "26", cw);
		tC31.setName("r26");
		C31.setEditor(new CellEditor(tC31));
		columns.add(C31);

		ColumnConfig C32 = new ColumnConfig("r27", "27", cw);
		tC32.setName("r27");
		C32.setEditor(new CellEditor(tC32));
		columns.add(C32);

		ColumnConfig C33 = new ColumnConfig("r28", "28", cw);
		tC33.setName("r28");
		C33.setEditor(new CellEditor(tC33));
		columns.add(C33);

		C34 = new ColumnConfig("r29", "29", cw);
		tC34.setName("r29");
		C34.setEditor(new CellEditor(tC34));
		columns.add(C34);

		C35 = new ColumnConfig("r30", "30", cw);
		tC35.setName("r30");
		C35.setEditor(new CellEditor(tC35));
		columns.add(C35);

		C36 = new ColumnConfig("r31", "31", cw);
		tC36.setName("r31");
		C36.setEditor(new CellEditor(tC36));
		columns.add(C36);
		
		ColumnConfig C37 = new ColumnConfig("average_length", "平均时长", 70);
		C37.setNumberFormat(NumberFormat.getFormat("#.#"));
		columns.add(C37);

		ColumnModel cm = new ColumnModel(columns);

		grid = new BaseGrid<BusiObject>(getStore(), cm, true, true) {
			public void addnewrecord() {
				if (tree_depcode.length() != 6) {
					MessageBox.alert("提示", "只能对小部门进行添加人员的排班，请重新选择，谢谢", null);
					return;
				}
				if (seach_by_date.getRawValue().equals("")) {
					MessageBox.alert("提示", "请选择要进行排班的月份，谢谢", null);
					seach_by_date.focus();
					return;
				}
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("depCode", tree_depcode);
				map.put("r1_r", false);
				map.put("r2_r", false);
				map.put("r3_r", false);
				map.put("r4_r", false);
				map.put("r5_r", false);
				map.put("r6_r", false);
				map.put("r7_r", false);
				map.put("r8_r", false);
				map.put("r9_r", false);
				map.put("r10_r", false);
				map.put("r11_r", false);
				map.put("r12_r", false);
				map.put("r13_r", false);
				map.put("r14_r", false);
				map.put("r15_r", false);
				map.put("r16_r", false);
				map.put("r17_r", false);
				map.put("r18_r", false);
				map.put("r19_r", false);
				map.put("r20_r", false);
				map.put("r21_r", false);
				map.put("r22_r", false);
				map.put("r23_r", false);
				map.put("r24_r", false);
				map.put("r25_r", false);
				map.put("r26_r", false);
				map.put("r27_r", false);
				map.put("r28_r", false);
				map.put("r29_r", false);
				map.put("r30_r", false);
				map.put("r31_r", false);
				
				map.put("ym", DateTimeFormat.getFormat("yyyy-MM-dd").parse(seach_by_date.getRawValue() + "-01"));
				getStore().add(map);
			}

			@Override
			public void deleterecord() {
				if (tree_depcode.length() != 6) {
					MessageBox.alert("提示", "只能对小部门进行操作，请重新选择！", null);
					return;
				}
				if (grid.getSelectionModel().getSelectedItem() == null){
					MessageBox.alert("提示", "请选择要删除的员工！", null);
					return;
				}
				String readonly = " and (" ;
				String bcodes = " and icode = '" + grid.getSelectionModel().getSelectedItem().get("icode")+"' ";
				for(int i = 1;i <= 31;i++){
					readonly += " r" + i + "_r" + " = 1 or ";
				}
				readonly = readonly.substring(0, readonly.length() - 3) + ")";
				String sqlReadonly = "select * from hr_Attendance_schedule where depCode='"+tree_depcode+"' and convert(varchar(7),ym,120)" +
										"='"+seach_by_date.getRawValue()+"'" + bcodes + readonly;
				final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
				greetingService.getDataSet(sqlReadonly, new AsyncCallback<DataSet>() {
					@Override
					public void onFailure(Throwable caught) {
						
					}

					@Override
					public void onSuccess(DataSet result) {
						if(result.getRowCount() > 0){
							MessageBox.alert("提示", "当前选中的排班已有封存的日期，不允许删除！", null);
							grid.getStore().reload();
							delete = false;
						}else{
							delete = true;
						}
					}
				});
				if(delete){
					super.deleterecord();
				}
			}
			
			
		};
		grid.addListener(Events.AfterEdit, getListener());
		grid.addPlugin(cbs);
		cbs.setSelectionMode(SelectionMode.MULTI);
		grid.setSelectionModel(cbs);
		grid.setTitle("排班信息");
		grid.setBorders(false);
		grid.addListener(Events.BeforeEdit, getListener());
		
		cut.setEnabled(false);
		MenuItem insert = new MenuItem();
		insert.setText("复制该行排班");
		insert.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.addrecord()));
		insert.addSelectionListener(new SelectionListener<MenuEvent>() {
			public void componentSelected(MenuEvent ce) {
				BusiObject gstore=grid.getSelectionModel().getSelectedItem();			
				String ym = seach_by_date.getRawValue();// 当前排班的年月
				int year = Integer.parseInt(ym.substring(0, 4));
				int month = Integer.parseInt(ym.substring(5, 7));
				int mds = DateUtil.getDaysOfMonth(year, month);// 当前排班月份的天数
				copy_array.clear();
				for(int i=1;i<mds+1;i++){
					if(gstore.get("r"+i)==null){
						copy_array.add("");
					}else{
						copy_array.add(gstore.get("r"+i).toString());
					}
				}
				if(copy_array.size()<1){
					cut.setEnabled(false);
				}else{
					cut.setEnabled(true);
				}
			}
		});
		
		cut.setText("粘贴排班");
		cut.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.fresh()));
		cut.addSelectionListener(new SelectionListener<MenuEvent>() {
			public void componentSelected(MenuEvent ce) {
				BusiObject gstore = grid.getSelectionModel().getSelectedItem();
				String ym = seach_by_date.getRawValue();// 当前排班的年月
				int year = Integer.parseInt(ym.substring(0, 4));
				int month = Integer.parseInt(ym.substring(5, 7));
				int mds = DateUtil.getDaysOfMonth(year, month);// 当前排班月份的天数
				
				for (BusiObject md : grid.getSelectionModel().getSelectedItems()) {
					int index = grid.getStore().indexOf(md);
					for(int i=1;i<mds+1;i++){
					  store.setValue(index, "r"+i, copy_array.get(i-1));
					}
				}	
			}
		});
		
		MenuItem delete = new MenuItem();
		delete.setText("批量删除排班");
		delete.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.deleterecord()));
		delete.addSelectionListener(new SelectionListener<MenuEvent>() {
			public void componentSelected(MenuEvent ce) {
				for(BusiObject bo : grid.getSelectionModel().getSelectedItems()){
					bcodesDel += bo.get("icode") + ",";
				}
				bcodesDel = bcodesDel.substring(0,bcodesDel.length() - 1) + ")";
				
				String readonly = " and (" ;
				for(int i = 1;i <= 31;i++){
					readonly += " r" + i + "_r" + " = 1 or ";
				}
				readonly = readonly.substring(0, readonly.length() - 3) + ")";
				String sqlReadonly = "select * from hr_Attendance_schedule where depCode='"+tree_depcode+"' and convert(varchar(7),ym,120)" +
				"='"+seach_by_date.getRawValue()+"'" + bcodesDel + readonly;
				System.out.println("--------->>>>>>" + sqlReadonly);
				final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
				greetingService.getDataSet(sqlReadonly, new AsyncCallback<DataSet>() {
				@Override
				public void onFailure(Throwable caught) {
				
				}
				
				@Override
				public void onSuccess(DataSet result) {
					if(result.getRowCount() > 0){
						MessageBox.alert("提示", "当前选中的排班已有封存的日期，不允许删除！", null);
						grid.getStore().reload();
					}else{
						String sql = "delete from hr_attendance_schedule where convert(varchar(7),ym,120) = '"+seach_by_date.getRawValue()+"' " + bcodesDel;
						save_sql(sql);
						grid.getStore().reload();
					}
					bcodesDel = " and icode in ( ";
				}
				});
			}
		});
		
		grid.getMenu().add(insert);
		grid.getMenu().add(cut);
		grid.getMenu().add(delete);
		grid.setBorders(true);
		grid.setLoadMask(true);
		grid.addListener(Events.OnClick, getListener());
	//	grid.setAutoExpandColumn("");
		Tools.setResize(grid, 0.98, 0.59);
	}

	/**
	 * 班次设定
	 */
	BaseStore<BusiObject> workBatch_store = null;

	private BaseStore<BusiObject> getworkBatch_store() {
		if (workBatch_store == null) {
			creat_eworkBatch_store();
		}
		return workBatch_store;
	}

	BaseStore<BusiObject> creat_eworkBatch_store() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("depCode");
		type.getField("depCode").setType(String.class);
		type.addField("batchName");
		type.getField("batchName").setType(String.class);
		type.addField("batchNo");
		type.getField("batchNo").setType(String.class);
		type.addField("bgnTime1");
		type.getField("bgnTime1").setType(String.class);
		type.addField("endTime1");
		type.getField("endTime1").setType(String.class);
		type.addField("bgnTime2");
		type.getField("bgnTime2").setType(String.class);
		type.addField("endTime2");
		type.getField("endTime2").setType(String.class);
		type.addField("Span1");
		type.getField("Span1").setType(String.class);
		type.addField("Span2");
		type.getField("Span2").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_Attendance_WorkBatch");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from hr_Attendance_WorkBatch");
		workBatch_store = new BaseStore<BusiObject>(type, info);
		return workBatch_store;
	}

	BaseGrid<BusiObject> workBatch_grid = null;

	BaseGrid<BusiObject> create_workBatch_grid() {
		CheckBoxSelectionModel<BusiObject> cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig C1 = new ColumnConfig("icode", "编号", 40);
		columns.add(C1);

		ColumnConfig C2 = new ColumnConfig("batchName", "班次名称", 60);
		columns.add(C2);
		
		ColumnConfig C3 = new ColumnConfig("batchNo", "班次代码", 60);
		columns.add(C3);

		ColumnConfig C4 = new ColumnConfig("bgnTime1", "起始1", 60);
		columns.add(C4);

		ColumnConfig C5 = new ColumnConfig("endTime1", "结束1", 60);
		columns.add(C5);
		
		ColumnConfig C8 = new ColumnConfig("Span1", "跨天1", 40);
		columns.add(C8);

		ColumnConfig C6 = new ColumnConfig("bgnTime2", "起始2", 60);
		columns.add(C6);

		ColumnConfig C7 = new ColumnConfig("endTime2", "结束2", 60);
		columns.add(C7);

		ColumnConfig C9 = new ColumnConfig("Span2", "跨天2", 40);
		columns.add(C9);

		ColumnModel cm = new ColumnModel(columns);
		workBatch_grid = new BaseGrid<BusiObject>(getworkBatch_store(), cm,
				false, false);
		workBatch_grid.setLoadMask(true);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		workBatch_grid.setSelectionModel(cbs);
		workBatch_grid.setBorders(true);
		Tools.setResize(workBatch_grid, 0.70, 0.30);
		return workBatch_grid;
	}
	
	/**
	 * 公共班次设定
	 */
	BaseStore<BusiObject> public_workBatch_store = null;

	private BaseStore<BusiObject> getPublicWorkBatch_store() {
		if (public_workBatch_store == null) {
			creat_publicWorkBatch_store();
		}
		return public_workBatch_store;
	}

	BaseStore<BusiObject> creat_publicWorkBatch_store() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("depCode");
		type.getField("depCode").setType(String.class);
		type.addField("batchName");
		type.getField("batchName").setType(String.class);
		type.addField("batchNo");
		type.getField("batchNo").setType(String.class);
		type.addField("bgnTime1");
		type.getField("bgnTime1").setType(String.class);
		type.addField("endTime1");
		type.getField("endTime1").setType(String.class);
		type.addField("bgnTime2");
		type.getField("bgnTime2").setType(String.class);
		type.addField("endTime2");
		type.getField("endTime2").setType(String.class);
		type.addField("Span1");
		type.getField("Span1").setType(String.class);
		type.addField("Span2");
		type.getField("Span2").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_Attendance_WorkBatch");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from hr_Attendance_WorkBatch where depCode = '00'");
		public_workBatch_store = new BaseStore<BusiObject>(type, info) {
			public void _LoadComplete() {
				super._LoadComplete();
				publicWorkBatch_grid.getSelectionModel().deselect(0);
			}
		};
		return public_workBatch_store;
	}

	BaseGrid<BusiObject> publicWorkBatch_grid = null;

	BaseGrid<BusiObject> create_publicWorkBatch_grid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig C1 = new ColumnConfig("icode", "编号", 40);
		columns.add(C1);

		ColumnConfig C2 = new ColumnConfig("batchName", "班次名称", 60);
		columns.add(C2);
		
		ColumnConfig C3 = new ColumnConfig("batchNo", "班次代码", 60);
		columns.add(C3);

		ColumnModel cm = new ColumnModel(columns);
		publicWorkBatch_grid = new BaseGrid<BusiObject>(getPublicWorkBatch_store(), cm,
				false, false);
		publicWorkBatch_grid.setLoadMask(true);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		publicWorkBatch_grid.setSelectionModel(cbs);
		cbs.deselectAll();
		publicWorkBatch_grid.setBorders(true);
		Tools.setResize(publicWorkBatch_grid, 0.27, 0.30);
		return publicWorkBatch_grid;
	}
	
	private boolean checkMonth() {
		if (month.equals("")) {
			MessageBox.alert("提示", "排班年月不能为空，请选择排班年月，谢谢",null);
			return false;
		}
		return true;
	}

	private String month;
	int sync = 0;
	Listener<BaseEvent> defaultlistener;
	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}
		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				month = seach_by_date.getRawValue();
				sync++;

				try {
					if (sync == 1) {
						if (tree_depcode == null) {
							MessageBox.alert("提示", "部门为空，请选择部门，再进行操作，谢谢", null);
							return;
						}
						
						if (be.getType() == Events.Select) {
							if (be.getSource().equals(get)) {
								do_all();
							} else if (be.getSource().equals(add_p)) {
								if (!checkMonth()) {
									return;
								}
								add_new_p();
							} else if (be.getSource().equals(seach_bt)) {
								seach_bar();
							} else if (be.getSource().equals(copy_p)) {
								if (!checkMonth()) {
									return;
								}
								copy_p();
							} else if(be.getSource().equals(print)){
								if(tree_depcode == null || month.equals("")){
									MessageBox.alert("警告", "请选择查询条件",null);
								}else if(tree_depcode.length() < 6){
									MessageBox.alert("提示", "打印只能选择小部门", null);
								}else{
									String s_name = field.getRawValue();
									String dep_code = tree_depcode;
									String data = month + "_" + dep_code;
									data += "_";
									data += s_name;
									Window.open(GWT.getHostPageBaseURL() + "hr_Jsp/rptScheduling.jsp?data=" + URL.encode(data),"_blank","menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes");
								}
							}
						} else if (be.getType() == Events.AfterEdit) {
							GridEvent<BusiObject> ge = (GridEvent<BusiObject>) be;
							index = ge.getRowIndex();
							
							if (ge.getProperty().equals("r1")) {// 可同时监听多个字段ge.getProperty().equals("amount")
								check_p(tC6, index);
							} else if (ge.getProperty().equals("r2")) {
								check_p(tC7, index);
							} else if (ge.getProperty().equals("r3")) {
								check_p(tC8, index);
							} else if (ge.getProperty().equals("r4")) {
								check_p(tC9, index);
							} else if (ge.getProperty().equals("r5")) {
								check_p(tC10, index);
							} else if (ge.getProperty().equals("r6")) {
								check_p(tC11, index);
							} else if (ge.getProperty().equals("r7")) {
								check_p(tC12, index);
							} else if (ge.getProperty().equals("r8")) {
								check_p(tC13, index);
							} else if (ge.getProperty().equals("r9")) {
								check_p(tC14, index);
							} else if (ge.getProperty().equals("r10")) {
								check_p(tC15, index);
							} else if (ge.getProperty().equals("r11")) {
								check_p(tC16, index);
							} else if (ge.getProperty().equals("r12")) {
								check_p(tC17, index);
							} else if (ge.getProperty().equals("r13")) {
								check_p(tC18, index);
							} else if (ge.getProperty().equals("r14")) {
								check_p(tC19, index);
							} else if (ge.getProperty().equals("r15")) {
								check_p(tC20, index);
							} else if (ge.getProperty().equals("r16")) {
								check_p(tC21, index);
							} else if (ge.getProperty().equals("r17")) {
								check_p(tC22, index);
							} else if (ge.getProperty().equals("r18")) {
								check_p(tC23, index);
							} else if (ge.getProperty().equals("r19")) {
								check_p(tC24, index);
							} else if (ge.getProperty().equals("r20")) {
								check_p(tC25, index);
							} else if (ge.getProperty().equals("r21")) {
								check_p(tC26, index);
							} else if (ge.getProperty().equals("r22")) {
								check_p(tC27, index);
							} else if (ge.getProperty().equals("r23")) {
								check_p(tC28, index);
							} else if (ge.getProperty().equals("r24")) {
								check_p(tC29, index);
							} else if (ge.getProperty().equals("r25")) {
								check_p(tC30, index);
							} else if (ge.getProperty().equals("r26")) {
								check_p(tC31, index);
							} else if (ge.getProperty().equals("r27")) {
								check_p(tC32, index);
							} else if (ge.getProperty().equals("r28")) {
								check_p(tC33, index);
							} else if (ge.getProperty().equals("r29")) {
								check_p(tC34, index);
							} else if (ge.getProperty().equals("r30")) {
								check_p(tC35, index);
							} else if (ge.getProperty().equals("r31")) {
								check_p(tC36, index);
							} else if (ge.getProperty().equals("bname")) {
								String bcode = box.getValue().get("extdisplayname");
								updateSchedulOfBcode(bcode);
							}

						} else if (be.getType() == Events.BeforeEdit) {
							final GridEvent<BusiObject> be2 = (GridEvent<BusiObject>)be;
							if (be2.getProperty().equals("r1")) {// 可同时监听多个字段ge.getProperty().equals("amount")
								setReadOnly(be2, "r1", tC6);
							} else if (be2.getProperty().equals("r2")) {
								setReadOnly(be2, "r2", tC7);
							} else if (be2.getProperty().equals("r3")) {
								setReadOnly(be2, "r3", tC8);
							} else if (be2.getProperty().equals("r4")) {
								setReadOnly(be2, "r4", tC9);
							} else if (be2.getProperty().equals("r5")) {
								setReadOnly(be2, "r5", tC10);
							} else if (be2.getProperty().equals("r6")) {
								setReadOnly(be2, "r6", tC11);
							} else if (be2.getProperty().equals("r7")) {
								setReadOnly(be2, "r7", tC12);
							} else if (be2.getProperty().equals("r8")) {
								setReadOnly(be2, "r8", tC13);
							} else if (be2.getProperty().equals("r9")) {
								setReadOnly(be2, "r9", tC14);
							} else if (be2.getProperty().equals("r10")) {
								setReadOnly(be2, "r10", tC15);
							} else if (be2.getProperty().equals("r11")) {
								setReadOnly(be2, "r11", tC16);
							} else if (be2.getProperty().equals("r12")) {
								setReadOnly(be2, "r12", tC17);
							} else if (be2.getProperty().equals("r13")) {
								setReadOnly(be2, "r13", tC18);
							} else if (be2.getProperty().equals("r14")) {
								setReadOnly(be2, "r14", tC19);
							} else if (be2.getProperty().equals("r15")) {
								setReadOnly(be2, "r15", tC20);
							} else if (be2.getProperty().equals("r16")) {
								setReadOnly(be2, "r16", tC21);
							} else if (be2.getProperty().equals("r17")) {
								setReadOnly(be2, "r17", tC22);
							} else if (be2.getProperty().equals("r18")) {
								setReadOnly(be2, "r18", tC23);
							} else if (be2.getProperty().equals("r19")) {
								setReadOnly(be2, "r19", tC24);
							} else if (be2.getProperty().equals("r20")) {
								setReadOnly(be2, "r20", tC25);
							} else if (be2.getProperty().equals("r21")) {
								setReadOnly(be2, "r21", tC26);
							} else if (be2.getProperty().equals("r22")) {
								setReadOnly(be2, "r22", tC27);
							} else if (be2.getProperty().equals("r23")) {
								setReadOnly(be2, "r23", tC28);
							} else if (be2.getProperty().equals("r24")) {
								setReadOnly(be2, "r24", tC29);
							} else if (be2.getProperty().equals("r25")) {
								setReadOnly(be2, "r25", tC30);
							} else if (be2.getProperty().equals("r26")) {
								setReadOnly(be2, "r26", tC31);
							} else if (be2.getProperty().equals("r27")) {
								setReadOnly(be2, "r27", tC32);
							} else if (be2.getProperty().equals("r28")) {
								setReadOnly(be2, "r28", tC33);
							} else if (be2.getProperty().equals("r29")) {
								setReadOnly(be2, "r29", tC34);
							} else if (be2.getProperty().equals("r30")) {
								setReadOnly(be2, "r30", tC35);
							} else if (be2.getProperty().equals("r31")) {
								setReadOnly(be2, "r31", tC36);
							}
							/*
							 * 排班新增加一条记录的时候，当名字改变的时候，bcode也对应改变
							 */
						} else if (be.getType() == Events.BeforeQuery) { 
							if(be.getSource().equals(box)){
								box.setFilter("where employstatus='在职' and a.bcode = b.bcode  and a.systemid = '"+tree_depcode+"' " +
										  "	and a.bcode not in (select isnull(bcode,'') from hr_attendance_schedule where depcode  " +
										  "	= '"+tree_depcode+"' and convert(varchar(7),ym,120)='"+ month +"')");
							}
							
						}else if(be.getType() == Events.Changed){
							if(box.getStore().getCount() != 0){
								//获取BaseComboBoxX中隐藏的字段bocde
								String employno = box.getValue().get("extdisplayname").toString();
								boolean isRepeat = false;
								for(int i = 0;i < grid.getStore().getCount();i++){
									String bcode = grid.getStore().getAt(i).get("bcode");
									String name = grid.getStore().getAt(i).get("bname");
									if(employno.equals(bcode)){
										MessageBox.alert("提示", "当前部门 " + name + " 已存在排班，不允许重复排班。", null);
										grid.getStore().reload();
										isRepeat = true;
										break;
									}
								}
								if(!isRepeat){
									if(employno != "" || employno != null){
										grid.getSelectionModel().getSelectedItem().set("bcode", employno);
										isRepeat = false;
									}
								}
							}
						}
						else if (be.getType() == Events.KeyUp) {
							if (be instanceof ComponentEvent) {
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{

								}
							}
						}else if(be.getType() == Events.OnClick){
							if(be.getSource().equals(grid)){
								if(grid.getSelectionModel().getSelectedItem() != null){
									String name = grid.getSelectionModel().getSelectedItem().get("bname");
									String bcode = grid.getSelectionModel().getSelectedItem().get("bcode");
									if(name == null){
										box.setReadOnly(false);
										grid.setTitle("你现在操作的是新添加的排班人员");
									}else {
										grid.setTitle("你现在操作的排班人员是" + name + "，编号为：" + bcode);
										box.setReadOnly(true);
									}
								}
							}
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}
	
	private void setReadOnly(GridEvent<BusiObject> be2, String field, TextField<String> tC6) {
		if (store.getValue("bname") == null) {
			return;
		}
		int i = be2.getRowIndex();
		boolean readOnly = (Boolean)store.getValue(i, field + "_r");
		tC6.setReadOnly(readOnly);
		if (readOnly) {
			Info.display("不能修改", "该天的排班已经封存，不能修改");
		}
	}

	String copy_month; // 记录前一个月份的值

	/**
	 * 该方法用于继承上月排班，如果该月已经排过班了，提示用户。如果上月还没有排班，也提示用户，并让用户
	 * 选择是否对上月进行排班的操作。优先提示用户当月是否排班的信息（也就是说如果上月没排班，当月已经排班，
	 * 就只提示用户当月已经排班的信息，不再提示用户上月没排班的信息）
	 */
	private void copy_p() {  
		if ((Integer.parseInt(seach_by_date.getRawValue().substring(5)) - 1) >= 10) {
			copy_month = seach_by_date.getRawValue().substring(0, 5)
					+ (Integer.parseInt(month.substring(5)) - 1);
		} else {
			copy_month = month.substring(0, 5)
					+ "0"
					+ (Integer.parseInt(month.substring(6)) - 1);
		}
		//GWT.log("@@@@:"+copy_month);
		if (tree_depcode == null || tree_depcode.length() < 5) { // 获得分部门id
			MessageBox.alert("警告", "请先选择一个分部门！", null);
		} else {
			MessageBox.confirm("提示	", "是否确定将分部门:'" + tree_de_name + "'中的"
					+ copy_month + "月排班数据导入作为" + month
					+ "月排班数据?", new Listener<MessageBoxEvent> (){
				public void handleEvent(MessageBoxEvent be) {
					if(be.getButtonClicked().getText().equals(GXT.MESSAGES.messageBox_yes()))
					{//选择同意后
						// 判断数据表中是否已近存在当前月的当前部门的排班
						String csql = "select  count(*) as c,ym from  hr_Attendance_schedule where  ym in ('"
								+ seach_by_date.getRawValue()
								+ "-01' , '"
								+ copy_month
								+ "-01') and depCode='"
								+ tree_depcode + "' group by ym";
						final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
								.create(com.base.myproject.client.tools.GreetingService.class);
						greetingService.getDataSet(csql,
								new AsyncCallback<DataSet>() {
									@Override
									public void onFailure(Throwable caught) {
									}

									@Override
									public void onSuccess(DataSet result) {
										if (result.getRowCount() == 2) {
											MessageBox.alert("警告",month + "月份已经有值了不能再次排班！",
													null);
										} else if (result.getRowCount() == 0) {
											MessageBox.confirm("提示	", copy_month
													+ "月没有排班，所以继承上月的排班操作不成功！是否确定将'" + tree_de_name
														+ "'部门中的人员导入并进行'" + copy_month + "'排班?", new Listener<MessageBoxEvent> (){
												public void handleEvent(MessageBoxEvent be) {
													if(be.getButtonClicked().getText().equals(GXT.MESSAGES.messageBox_yes()))
													{//选择同意后
														submitAndUpdateSchedulOfOneMonth(copy_month);
													}//否的时候
												}		
												});//第一次是否提示结束
											
										} else {
											if (result.getValue(0, "ym").substring(0, 7).equals(month)) {
												MessageBox.alert("警告",	month	+ "月份已经有值了不能再次排班！",
																null);
											} else {
												submitAndUpdateSchedulOfOneMonth(month, true);
											}
										}

									}
								});
					}
					}		
					});
		 }
	}

	/**************************************/
	private void add_new_p() {
		if (tree_depcode == null || tree_depcode.length() < 5) { // 获得分部门id
			MessageBox.alert("警告", "请先选择一个分部门！", null);
		} else {
			final Listener<MessageBoxEvent> l = new Listener<MessageBoxEvent>() {
				public void handleEvent(MessageBoxEvent ce) {
					Button btn = ce.getButtonClicked();
					if (btn.getText().equals("是")) {
						// 判断数据表中是否已近存在当前月的当前部门的排班
						String csql = "select  count(*) as c from  hr_Attendance_schedule where ym='" + month + "-01' and depCode='"
								+ tree_depcode + "'";
						final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
								.create(com.base.myproject.client.tools.GreetingService.class);
						greetingService.getDataSet(csql,
								new AsyncCallback<DataSet>() {
									@Override
									public void onFailure(Throwable caught) {
									}

									@Override
									public void onSuccess(DataSet result) {
										if (result.getValue(0, "c").equals("0")) {// 返回结果为0
											submitAndUpdateSchedulOfOneMonth(month);
										} else {
											MessageBox.alert("警告",
													"此部门当月已经排班,请不要重复排班！", null);
										}
									}
								});
					}
				}
			};
			MessageBox.confirm("提示信息", "是否确定将'" + tree_de_name
					+ "'部门中的人员导入并进行'" + seach_by_date.getRawValue() + "'排班?", l);
		}

	}
	
	/**
	 * 实现对一个部门的现有在职人员进行排班
	 * @param month 要进行排班的月份
	 */
	private void submitAndUpdateSchedulOfOneMonth(String month, boolean isExtendsOfLastMonth) {
		sqlList.clear();
		String insertSql = "";
		if (isExtendsOfLastMonth) {
			insertSql = "insert  hr_Attendance_schedule (ym,bcode,bname,depCode," +
			"r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19," +
			"r20,r21,r22,r23,r24,r25,r26,r27,r28,r29,r30,r31) select '"
			+ month
			+ "-01',bcode,bname,depCode,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19," +
			"r20,r21,r22,r23,r24,r25,r26,r27,r28,r29,r30,r31 from hr_Attendance_schedule where depCode='"
			+ tree_depcode
			+ "' and ym = '"
			+ copy_month + "-01'";
		} else {
			insertSql = "insert  hr_Attendance_schedule (ym,bcode,bname,depCode) select '"
				+ month
				+ "-01"
				+ "',bcode.bcode,bcode.bname,bcode.systemid from bcode,bcode_job where bcode.bcode=bcode_job.bcode and employstatus='在职' and systemid='"
				+ tree_depcode + "'";
		}
		sqlList.add(insertSql);
		//updateSchedulOfOneMonth(month);  //跟请假结合的话，请注解下面那句，打开上面这句
		save_sql(sqlList, false, month);
	}
	
	private void submitAndUpdateSchedulOfOneMonth(String month) {
		submitAndUpdateSchedulOfOneMonth(month, false);
	}
	
	/**
	 * 更新所有这个部门这个月请过假的员工和请假日期
	 */
	private void updateSchedulOfOneMonth(final String month) {
		String sql = "select * from vacationDetail where exists (select 1 from bcode, bcode_job where bcode.bcode = bcode_job.bcode and bcode.bcode = vacationdetail.bcode and systemid = '" 
			+ tree_depcode + "') and convert(varchar(7), vacationDay, 120) = '" + month + "'";
		
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.getDataSet(sql, new AsyncCallback<DataSet>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(DataSet result) {
				String where = " where ym = '" + month + "-01' and depCode = '" + tree_depcode + "'";
				StringBuilder updateSql = new StringBuilder();
				updateSql.append("update hr_Attendance_schedule set ");
				
				int size = result.getRowCount();
				
				if (size > 0) {
					String bcode = result.getValue(0, "bcode");
					for (int i=0; i<size; i++) {
						int vacationDay = Integer.parseInt(result.getValue(i, "vacationDay").substring(8, 10));
						String code = result.getValue(i, "code");
						
						if (!bcode.equals(result.getValue(i, "bcode"))) {
							int sqlLength = updateSql.length();
							updateSql.deleteCharAt(sqlLength - 1);
							updateSql.append(where.toString());
							updateSql.append(" and bcode = '");
							updateSql.append(bcode);
							updateSql.append("'");
							sqlList.add(updateSql.toString());
							updateSql.delete(34, updateSql.length());
							
							bcode = result.getValue(i, "bcode");
						}
						updateSql.append("r");
						updateSql.append(vacationDay);
						updateSql.append("='");
						updateSql.append(code);
						updateSql.append("',r");
						updateSql.append(vacationDay);
						updateSql.append("_r=1,");
						
						if (i == size - 1) {
							int sqlLength = updateSql.length();
							updateSql.deleteCharAt(sqlLength - 1);
							updateSql.append(where.toString());
							updateSql.append(" and bcode = '");
							updateSql.append(bcode);
							updateSql.append("'");
							sqlList.add(updateSql.toString());
						}
					}
				}
				save_sql(sqlList, false, month);
			}
		});
	}
	
	/**
	 * 更新某个员工的请假排班
	 */
	private void updateSchedulOfBcode(String bcode) {
		String sql = "select * from vacationDetail where bcode = '" + bcode + "' and convert(varchar(7), vacationDay, 120) = '" + month + "'";
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.getDataSet(sql, new AsyncCallback<DataSet>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(DataSet result) {
				for (int i=0; i<result.getRowCount(); i++) {
					int vacationDay = Integer.parseInt(result.getValue(i, "vacationDay").substring(8, 10));
					grid.setValue(index, "r" + vacationDay, result.getValue(i, "code"));
					grid.setValue(index, "r" + vacationDay + "_r", true);
				}
			}
		});
	}

	/**
	 * 向服务器发送insert的sql语句，增加新的排班信息！
	 * @param sql 要发送到服务器的insert语句
	 * @return 是否insert成功
	 */
	private void save_sql(String sql) {
		List<String> list = new ArrayList<String>();
		list.add(sql);
		save_sql(list, false, seach_by_date.getRawValue());
	}
	
	MessageBox update = null;
	private boolean save_sql(List<String> lists, final boolean isSave, final String month) {
		CommandSyncsql csql = new CommandSyncsql();// 执行sql命令
		for (String sql : lists) {
			csql.getV().add(sql);
		}
		if (csql.getV().size() <= 0) {
			return false;
		} else {
			CommandSyncContainer list = new CommandSyncContainer();// 可以添加的命令
			list.add(csql);
			if (isSave) {
				update = MessageBox.wait("计算",  
			            "计算平均时长中请等待...", "计算中...");  
			}

			GreetingServiceAsync greetingService = GWT
					.create(GreetingService.class);
			greetingService.SendCommandSync(list,
					new AsyncCallback<CommandSyncReturnObject>() {
						@Override
						public void onFailure(Throwable caught) {
							MessageBox.alert("错误", "网络连接失败", null);
						}

						@Override
						public void onSuccess(CommandSyncReturnObject result) {
							if (result.isB()) {
								if (isSave) {
									update.close();
									grid.getStore().reload();
								} else {
									Info.display("提示信息", "排班操作成功");
									// 跟新grid,显示新增加排班的内容
									StoreInfo info2 = new StoreInfo();
									info2.setTablename("hr_Attendance_schedule");
									info2.setKeyname(new String[] { "icode" });
									info2.setSql("select * from hr_Attendance_schedule where depCode = '"
											+ tree_depcode + "' and  ym='" + month
											+ "-01'");//
									grid.getStore().setStoreinfo(info2);
									grid.getStore().reload();
									grid.getView().refresh(true);
								}
							} else {
								MessageBox.alert("错误", "错误，错误代码"
										+ result.getMessage(), null);

							}
						}
					});
		}
		return true;
	}

	public void to_set_values(String sql, final String fd) {
		// 得到上面rpc的结果 判断当月某日是否为法定 并复制《但是 程序是先执行了这里 才执行上面的rpc，因此这里的假期数组为空
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.getDataSet(sql, new AsyncCallback<DataSet>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(DataSet result) {
				if (result.getValue(0, "r1").equals("0")) {// 返回结果为0
					Info.display("错误信息提示！", "批量排班出错!");
				} else {
					sql_1 = "";// 清空全局sql1 防止多次处理 结果累加错误
					int week = 0;
					String ban;
					boolean bl = true;
					for (BusiObject md : grid.getSelectionModel()
							.getSelectedItems()) {
						bl = false;
						int index = grid.getStore().indexOf(md);
						for (int p = 1; p <= m_day; p++) {

							if (fd == "1") {
								if (Integer.parseInt(holiday_p[p - 1]) > 0) {
									ban = "PH";
								} else {
									int w = Integer.parseInt(result.getValue(0,
											"r" + p));
									if (w == 1) {
										w = w + 7;
									}
									if (checkGroup.get(w - 1).getRawValue() == "true") {
										ban = "DO";
									} else {
										ban = batchNo;
									}
								}
							} else {
								int w = Integer.parseInt(result.getValue(0, "r"
										+ p));
								if (w == 1) {
									w = w + 7;
								}
								if (checkGroup.get(w - 1).getRawValue() == "true") {
									ban = "DO";
								} else {
									ban = batchNo;
								}
							}
							if (grid.getValue(index, "r" + p) == null)
								grid.setValue(index, "r" + p, ban);
						}
					}
					if (bl) {
						MessageBox.alert("警告", "您还没有选择需要排班的人员！", null);
						return;
					}
				}
			}
		});

	}

	// 批量排班
	String batchNo;// 班次表单选的 班次代码
	int m_day;// 指定月份的天数
	String[] holiday_p;// 指定月份中的所有天数的对应星期数
	String aa;
	String sql_2 = "";
	String sql_1 = "";

	private void do_all() {

		if (tree_depcode == null || tree_depcode.length() < 5) {// 判断是否为分部门
			MessageBox.alert("警告", "请先选择一个分部门！", null);
			return;
		}
		/*
		 * 获得单选grid中选中的内容
		 */
		BusiObject bo1 = workBatch_grid.getSelectionModel().getSelectedItem();
		BusiObject bo2 = publicWorkBatch_grid.getSelectionModel().getSelectedItem();
		
		if (bo1 != null && bo2 != null) {
			MessageBox.alert("提示", "只能选择一个班次，已经默认帮你把右边的公共班次清空了，你可以先排非公共班次，谢谢", null);
			publicWorkBatch_grid.getSelectionModel().deselectAll();
			return;
		}
		
		if (bo1 != null) {
			batchNo = bo1.get("batchNo").toString();
		} else if (bo2 != null) {
			batchNo = bo2.get("batchNo").toString();
		} else {
			MessageBox.alert("警告", "请选择一个班次！", null);
			return;
		}
		/*
		 * 循环获得多选grid中选中的内容
		 */
		// 1--7 :星期天:1，星期一：2，星期六:7
		// 测试：定义星期六，日 自动排班
		String ym = seach_by_date.getRawValue();// 当前排班的年月
		int year = Integer.parseInt(ym.substring(0, 4));
		int month = Integer.parseInt(ym.substring(5, 7));
		m_day = DateUtil.getDaysOfMonth(year, month);// 当前排班月份的天数
		String[] w_array = new String[m_day];// new一个当前月份长度的数组用来保存每天是星期几
		holiday_p = w_array;

		for (int y = 1; y <= m_day; y++) {
			if (y == m_day) {
				sql_1 = sql_1 + " datepart(weekday,'" + ym + "-" + y
						+ "') as r" + y;
			} else {
				sql_1 = sql_1 + " datepart(weekday,'" + ym + "-" + y
						+ "') as r" + y + ", ";
			}
			if (y < 10) {
				sql_2 = sql_2
						+ " (select  count(*) from  hr_Attendance_holiday  where '"
						+ ym + "-0" + y
						+ "' between bgnDate and  endDate) as h" + y + ", ";
			} else {
				if (y == m_day) {
					sql_2 = sql_2
							+ " (select  count(*) from  hr_Attendance_holiday  where '"
							+ ym + "-" + y
							+ "' between  bgnDate and  endDate) as h" + y;
				} else {
					sql_2 = sql_2
							+ " (select  count(*) from  hr_Attendance_holiday  where '"
							+ ym + "-" + y
							+ "' between  bgnDate and  endDate) as h" + y
							+ ", ";
				}
			}
		}
		sql_1 = "SELECT " + sql_1;
		sql_2 = "SELECT " + sql_2;
		// 这个rpc得到当月中是否有假期, 数组里面保存010101 0为普通日期 1为法定日期
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService2.getDataSet(sql_2, new AsyncCallback<DataSet>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(DataSet result) {
				String fd;
				if (checkBox8.getRawValue() == "true") {
					for (int i = 0; i < m_day; i++) {
						holiday_p[i] = result.getValue(0, "h" + (i + 1));
					}
					fd = "1";// 考虑法定节日
				} else {
					fd = "0";// 不考虑法定节日
				}

				sql_2 = "";// 清空全局sql1 防止多次处理 结果累加错误
				to_set_values(sql_1, fd);// 调用第二个rpc处理赋值

			}
		});
	}

	String Bno = "";

	public void check_p(TextField<String> te, final int index) {
		if (te.getValue() != null) {
			Bno = te.getValue().toUpperCase().replaceAll(" ", "");
		} else {
			Bno = "NO NUMBER";
		}
		// 判断当前输入的班次号码是否在班次表中存在
		String sql = "SELECT count(*) as c FROM hr_Attendance_WorkBatch WHERE (depCode LIKE '"
				+ tree_depcode
				+ "%' or depCode='00') AND batchNo='"
				+ Bno
				+ "' ";
		TextField_temp = te;
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.getDataSet(sql, new AsyncCallback<DataSet>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(DataSet result) {
				if (result.getValue(0, "c").equals("0")) {// 返回结果为0
					Info.display("", "输入的班次代码不存在！");
					grid.setValue(index, TextField_temp.getName(), "");
				} else {
					grid.setValue(index, TextField_temp.getName(), Bno);
				}
			}
		});

	}

	/**
	 * 过滤
	 * 
	 * @param store
	 * @param parent
	 * @param record
	 * @param property
	 * @param filter
	 * @return ajax 查询
	 */
	protected boolean Select(Store<BusiObject> store, BusiObject parent,
			BusiObject record, String property, String filter) {
		for (int i = 0; i < type_m.getFieldCount(); i++) {

			Object name = record.get(type_m.getField(i).getName());

			// TODO
			if (name != null
					&& name.toString().indexOf(filter.toLowerCase()) != -1) {
				return true;
			}
		}

		return false;
	}

	public void cread_field() {
		field = new StoreFilterField<BusiObject>() {
			@Override
			protected boolean doSelect(Store<BusiObject> store,
					BusiObject parent, BusiObject record, String property,
					String filter) {

				return Select(store, parent, record, property, filter);
			}

			@Override
			protected void onFilter() {
				super.onFilter();

				// grid.getSelectionModel().select(0);
			}

		};
		field.setAutoWidth(true);
		field.setToolTip("对当前部门排班进行查找");
		field.bind(getStore());
		this.field = field;
	}

	/************************************/
	public void seach_bar() {
		String where;
		StoreInfo info2 = new StoreInfo();
		info2.setTablename("hr_Attendance_schedule");
		info2.setKeyname(new String[] { "icode" });
		if (seach_by_date.getValue() == null) {
			where = " 1=1 and ";
		} else {
			where = " CONVERT(varchar(7),   ym,120)='"
					+ month + "' and ";
			setGridField();
		}
		if (tree_depcode.length() <= 4) {// 为父节点 2级节点
			info2.setSql("select * from hr_Attendance_schedule where " + where
					+ " depCode like '" + tree_depcode + "%25'");
		} else if (tree_depcode.length() > 4) {// 为子节点 3级节点
			info2.setSql("select * from hr_Attendance_schedule where " + where
					+ " depCode = '" + tree_depcode + "'");
		}
		grid.getStore().setStoreinfo(info2);
		grid.getStore().reload();
		grid.getView().refresh(true);
	}

	private void updateTimeOutOfSchedance() {
		List<BusiObject> list = store.getModels();
		List<String> updateSql = new ArrayList<String>();
		String sql = "";
		for (BusiObject model : list) {
			String bcode = model.get("bcode");
			Date ym = model.get("ym");
			
			//得到某条排班的年月
			int year = ym.getYear() + 1900;
			int month = ym.getMonth() + 1;
			String monthStr = month < 10 ? "0" + month : String.valueOf(month);
			String date = String.valueOf(year) + "-" + monthStr;
			
			if (bcode == null) continue;
			sql = "update hr_Attendance_schedule set average_length = (select avg(working_hours) from " +
					"hr_Attendance_WorkBatch a,v_schedule b where a.depCode = b.depCode and " +
					"a.batchNo = b.bc and b.bcode = '" + bcode + "' and CONVERT(varchar(7),   ymd,120) = '" + 
					date + "') where depCode = '" + model.get("depCode") + "' and bcode = '" + 
					bcode + "' and CONVERT(varchar(7),   ym,120) = '" + date + "'";
			
			updateSql.add(sql);
		}
		
		String deleteSql = "delete hr_Attendance_schedule where bcode is null and depCode='"+tree_depcode+"'";
		updateSql.add(deleteSql);
		save_sql(updateSql, true, seach_by_date.getRawValue());
	}
	
	/********************************************/
	private int index = 0; //记录现在操作的是第几行的数据

	protected void save() {
		store.trySave();
		updateTimeOutOfSchedance();
	}

}
